var gaodeMap = (function(){
  var M = {};
  var map;
  var areasPolygon=[];
  var currPolygonOptions;
  var currType;
  
  var normalLayer = new AMap.TileLayer()
  var satelliteLayer = new AMap.TileLayer.Satellite();
  var roadNetLayer =  new AMap.TileLayer.RoadNet();
  var xyzTileLayer = new AMap.TileLayer({
    getTileUrl: 'https://wprd0{1,2,3,4}.is.autonavi.com/appmaptile?x=[x]&y=[y]&z=[z]&size=1&scl=1&style=8&ltype=11',
    zIndex: 100
  });
  var dxTileLayer = new AMap.TileLayer({
    getTileUrl: 'https://t5.tianditu.gov.cn/ter_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=ter&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILECOL=[x]&TILEROW=[y]&TILEMATRIX=[z]&tk=989c234a26456547bec07a5211e754d6',
    zIndex: 1
  });
  var dxnTileLayer = new AMap.TileLayer({
    getTileUrl: 'https://t5.tianditu.gov.cn/cva_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cva&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILECOL=[x]&TILEROW=[y]&TILEMATRIX=[z]&tk=989c234a26456547bec07a5211e754d6',
    zIndex: 100
  });
  
  map = new AMap.Map('container', {
    resizeEnable: true,
    zooms: [1, 18],
    mapStyle: 'amap://styles/d08a25f37661a917ceca37fc7ab4f24f', //'amap://styles/darkblue',
    features: ['bg', 'road', 'building', 'point'],
    showLabel: true
  });
  map.setCity("郴州");
  map.on("complete", function(){
     map.setZoom(9);
  });
  
  window.map = map;
  M.init = function(){
    map.clearMap();
    M.addAreas();
  }
  M.setNormalLayer = function(tags){
    map.setLayers([normalLayer]);
    currType = null;
    M.setFeatures(tags);
  };
  M.setSatelliteLayer = function(tags){
     map.setLayers([satelliteLayer]);
     currType = 'satelliteLayer';
     M.setFeatures(tags);
  }
  M.setDxLayer = function(tags){
     map.setLayers([dxTileLayer]);
     currType = 'dxTileLayer';
     M.setFeatures(tags);
  }
  M.setFeatures = function(tags){
    console.log(currType)
    if(!currType){
      map.setFeatures(tags);
    }else {
      var tempBgLayer = dxTileLayer;
      if(currType == 'satelliteLayer'){
        tempBgLayer = satelliteLayer;
      }
      if(tags.indexOf("bg") == -1){
        map.remove(tempBgLayer);
      }else{
        map.remove(tempBgLayer);
        map.add(tempBgLayer);
      }
      if(tags.indexOf("point") == -1){
        map.remove(dxnTileLayer);
      }else{
        map.remove(dxnTileLayer);
        map.add(dxnTileLayer);
      }
      if(tags.indexOf("road") == -1){
        map.remove(roadNetLayer);
      }else{
        map.remove(roadNetLayer);
        map.add(roadNetLayer);
      }
    }
  }
  M.addAreas = function(){
//    var infoWindowContent =
//        '<div className="custom-infowindow input-card" class="foo--inside1" style="width:515px;height:600px">' +
//            '<div class="main">\
//        <table class="tab">\
//        <tbody>\
//      <tr><td>A：</td><td>7965</td></tr><tr><td>编号：</td><td>431021000196</td></tr><tr><td>名称：</td><td>下落水库</td></tr><tr><td>地点：</td><td>银河乡</td></tr><tr><td>年份：</td><td></td></tr><tr><td>B：</td><td>1958</td></tr><tr><td>型号：</td><td>小（1）型</td></tr><tr><td>C：</td><td>50</td></tr><tr><td>D：</td><td>118.1</td></tr><tr><td>E：</td><td>100</td></tr><tr><td>库容：</td><td>210</td></tr><tr><td>F：</td><td>6</td></tr><tr><td>G：</td><td>431021000171</td></tr><tr><td>H：</td><td>桂阳县银河乡水管站</td></tr><tr><td>I：</td><td>水利部门</td></tr><tr><td>J：</td><td>县</td></tr><tr><td>K：</td><td>PYD52433-X</td></tr></tbody></table>\
//    </div>' +
//        '</div>';

//    var infoWindow = new AMap.InfoWindow({
//        position: map.getCenter(),
//        offset: new AMap.Pixel(0, -35),
//        content: infoWindowContent,
//    });
//    infoWindow.open(map);
//    
//    setTimeout(function(){
//      var Scrollbar = window.Scrollbar;
//      Scrollbar.init(document.querySelector('.foo--inside1'));
//    }, 1000);
    
    if(null != areasPolygon){
      map.remove(areasPolygon);
    }
    areaInfos.forEach(function(areaInfo){
      M.local.addPolygon(areaInfo);
    });
  };
  M.removeAreas = function(){
    if(null != areasPolygon){
      map.remove(areasPolygon);
    }
  };
  
  M.local = {};
  
  M.local.addPolygon = function(areaInfo){
    var polygon = new AMap.Polygon({
      path: eval(areaInfo.points),
      strokeColor: '#555',
      strokeWeight: 1,
      fillColor: areaInfo.color,
      fillOpacity: 0,
      extData: areaInfo
    });
    areasPolygon.push(polygon);
    polygon.on("mouseover1", function(e){
      var tem = e.target.getOptions();
      var zoom = map.getZoom();
      if(zoom > 9) {
        return;
      }
      if(currPolygonOptions && tem.fillColor == 'green'){
        e.target.setOptions(currPolygonOptions);
      }else{
        currPolygonOptions = tem;
      }
      e.target.setOptions({
        strokeColor: 'blue',
        strokeWeight: 2,
        fillColor: 'green'
      });
    });
    polygon.on("mouseout1", function(e){
      var zoom = map.getZoom();
      if(zoom > 9) {
        return;
      }
      e.target.setOptions(currPolygonOptions);
    });
    polygon.on("dblclick", function(e){
      var d = e.target.getExtData();
      map.setZoomAndCenter(10, d.center)
    });
    map.add(polygon);
  };
  return M;
})();